package com.example.courseapp.adapter;

import android.annotation.SuppressLint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.courseapp.R;
import com.example.courseapp.bean.FoodInfo;
import com.example.courseapp.bean.SportInfo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class SportAdapter extends RecyclerView.Adapter<SportAdapter.ViewHolder> {

    List<SportInfo> courseList = new ArrayList<>();
    HashMap<String, SportInfo> map = new HashMap<>();

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.food_item, parent, false);
        return new ViewHolder(view);
    }


    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, @SuppressLint("RecyclerView") int position) {
        SportInfo course = courseList.get(position);
        holder.food.setText(course.getName() + " " +"半小时*"+course.number);
    }

    @Override
    public int getItemCount() {
        return courseList.size();
    }

    @SuppressLint("NotifyDataSetChanged")
    public void bindData(List<SportInfo> courseList) {
        this.courseList.clear();
        map.clear();
        for (int i = 0; i < courseList.size(); i++) {
            SportInfo foodInfo = courseList.get(i);
            if (map.containsKey(foodInfo.getName())) {
                foodInfo.number += 1;
                map.put(courseList.get(i).getName(), foodInfo);
            }else {
                foodInfo.number =1;
                map.put(courseList.get(i).getName(), foodInfo);
            }
        }
        map.forEach((string, foodInfo) -> {
            this.courseList.add(foodInfo);
        });
        notifyDataSetChanged();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        TextView food;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            food = itemView.findViewById(R.id.food);
        }
    }
}
